package org.dong.Tool;

import org.dong.Pojo.HttpRequest;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class ControllerReflex {

    public ControllerReflex(HttpRequest request, OutputStream os) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        // 读取配置文件
        Properties props = new Properties();
        props.load(new FileInputStream("Config.properties"));

        // 获取需要反射的类的信息
        String className = props.getProperty(request.getUrl());
        String methodName = props.getProperty("methodName");
//        System.out.println(className);
//        System.out.println(methodName);

        // 使用反射机制动态加载类
        Class<?> cls = Class.forName(className);
//        System.out.println(cls);
        Object obj = cls.newInstance();

        // 获取需要调用的方法
        Method method = cls.getMethod(methodName,HttpRequest.class, OutputStream.class);
//        System.out.println(method);
        // 调用方法
        Object invoke = method.invoke(obj,request,os);
    }

}
